#include "detect_level.h"

#include "stdio.h"
#include "string.h"


bool gpio_back_dis = 1, gpio_string_dis = 1;
const char gpio_pull_string[3][10] = {"PULL_N", "PULL_U", "PULL_D"};
extern kkui_menutab_info_s menu_detect_level_channel_set[];

uint8_t gpio_pull_set[8] = {0, 0, 0, 0, 0, 0, 0, 0};
// 电平检测功能相关程序
void detect_level_gpio_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
		
	GPIO_InitStruct.Pin = CH1_Pin;
	GPIO_InitStruct.Pull = gpio_pull_set[0];
	HAL_GPIO_Init(CH1_GPIO_Port, &GPIO_InitStruct);
	GPIO_InitStruct.Pin = CH2_Pin;
	GPIO_InitStruct.Pull = gpio_pull_set[1];
	HAL_GPIO_Init(CH2_GPIO_Port, &GPIO_InitStruct);
	GPIO_InitStruct.Pin = CH3_Pin;
	GPIO_InitStruct.Pull = gpio_pull_set[2];
	HAL_GPIO_Init(CH3_GPIO_Port, &GPIO_InitStruct);
	GPIO_InitStruct.Pin = CH4_Pin;
	GPIO_InitStruct.Pull = gpio_pull_set[3];
	HAL_GPIO_Init(CH4_GPIO_Port, &GPIO_InitStruct);
	GPIO_InitStruct.Pin = CH5_Pin;
	GPIO_InitStruct.Pull = gpio_pull_set[4];
	HAL_GPIO_Init(CH5_GPIO_Port, &GPIO_InitStruct);
	GPIO_InitStruct.Pin = CH6_Pin;
	GPIO_InitStruct.Pull = gpio_pull_set[5];
	HAL_GPIO_Init(CH6_GPIO_Port, &GPIO_InitStruct);
	GPIO_InitStruct.Pin = CH7_Pin;
	GPIO_InitStruct.Pull = gpio_pull_set[6];
	HAL_GPIO_Init(CH7_GPIO_Port, &GPIO_InitStruct);
	GPIO_InitStruct.Pin = CH8_Pin;
	GPIO_InitStruct.Pull = gpio_pull_set[7];
	HAL_GPIO_Init(CH8_GPIO_Port, &GPIO_InitStruct);
}


void detect_level_gpio_deinit(void)
{
	HAL_GPIO_DeInit(CH1_GPIO_Port, CH1_Pin);
	HAL_GPIO_DeInit(CH2_GPIO_Port, CH2_Pin);
	HAL_GPIO_DeInit(CH3_GPIO_Port, CH3_Pin);
	HAL_GPIO_DeInit(CH4_GPIO_Port, CH4_Pin);
	HAL_GPIO_DeInit(CH5_GPIO_Port, CH5_Pin);
	HAL_GPIO_DeInit(CH6_GPIO_Port, CH6_Pin);
	HAL_GPIO_DeInit(CH7_GPIO_Port, CH7_Pin);
	HAL_GPIO_DeInit(CH8_GPIO_Port, CH8_Pin);
}

#define GPIO_CH_VAL(_pos, _ch)	((GPIO_CH_DATA_S[(_pos)] >> (_ch)) & 0x01)

void detect_level_func(kkui_menu_event_e event, char* tab_name, void* param)
{
	char dis_buf[32];
	int ret = 0;
	uint8_t dis_ch;
	uint8_t pin_val[8];

	static int8_t level_func_mode = 0;
	static uint8_t GPIO_CH_DATA_S[128] = {0};

	if(event == ENTER_TAG)
	{
		detect_level_gpio_init();
		memset(GPIO_CH_DATA_S, 0, sizeof(GPIO_CH_DATA_S));
		memset(pin_val, 0, sizeof(pin_val));
	}
	
	if(event == BACK_LIST)
	{
		detect_level_gpio_deinit();
		memset(GPIO_CH_DATA_S, 0, sizeof(GPIO_CH_DATA_S));
		memset(pin_val, 0, sizeof(pin_val));
	}

	if(event == LOOP_RUN)
	{
		uiDisClear(0);
		pin_val[0]=HAL_GPIO_ReadPin(CH1_GPIO_Port, CH1_Pin);
		pin_val[1]=HAL_GPIO_ReadPin(CH2_GPIO_Port, CH2_Pin);
		pin_val[2]=HAL_GPIO_ReadPin(CH3_GPIO_Port, CH3_Pin);
		pin_val[3]=HAL_GPIO_ReadPin(CH4_GPIO_Port, CH4_Pin);
		pin_val[4]=HAL_GPIO_ReadPin(CH5_GPIO_Port, CH5_Pin);
		pin_val[5]=HAL_GPIO_ReadPin(CH6_GPIO_Port, CH6_Pin);
		pin_val[6]=HAL_GPIO_ReadPin(CH7_GPIO_Port, CH7_Pin);
		pin_val[7]=HAL_GPIO_ReadPin(CH8_GPIO_Port, CH8_Pin);
		
		GPIO_CH_DATA_S[0] = 0;
		GPIO_CH_DATA_S[0] |= pin_val[0];
		GPIO_CH_DATA_S[0] |= pin_val[1] << 1;
		GPIO_CH_DATA_S[0] |= pin_val[2] << 2;
		GPIO_CH_DATA_S[0] |= pin_val[3] << 3;
		GPIO_CH_DATA_S[0] |= pin_val[4] << 4;
		GPIO_CH_DATA_S[0] |= pin_val[5] << 5;
		GPIO_CH_DATA_S[0] |= pin_val[6] << 6;
		GPIO_CH_DATA_S[0] |= pin_val[7] << 7;


		if(level_func_mode == 0)
		{
			for(ret = 0; ret < 8; ret ++)
			{
				if(gpio_string_dis)
				{
					sprintf(dis_buf, "CH%d:%d ", ret + 1, pin_val[ret]);
					uiShowString(20+(ret%2)*50, 50-(ret/2)*16, dis_buf, 12, 1);
				}
				uiDrawRBox(16+(ret%2)*50, 49-(ret/2)*16, 38, 14, 4, 1);
				if(pin_val[ret] && gpio_back_dis)
					uiDrawRBlock(16+(ret%2)*50 + 2, 49-(ret/2)*16 + 2, 34, 10, 2, 2);
			}
		}
		if(level_func_mode == 1)
		{	
			if(gpio_string_dis)
			{
				uiShowString(1, 5 + 0 * 16, "CH7", 8, 1);
				uiShowString(1, 5 + 1 * 16, "CH5", 8, 1);
				uiShowString(1, 5 + 2 * 16, "CH3", 8, 1);
				uiShowString(1, 5 + 3 * 16, "CH1", 8, 1);
			}

			if(gpio_back_dis)
			{
				for(ret = 0; ret < (gpio_string_dis?105:126); ret ++)
				{
					if(ret % 3 == 0)
					{
						uiDrawPoint(127 - ret, 8 + 0 * 16, 1);
						uiDrawPoint(127 - ret, 8 + 1 * 16, 1);
						uiDrawPoint(127 - ret, 8 + 2 * 16, 1);
						uiDrawPoint(127 - ret, 8 + 3 * 16, 1);
					}
				}
			}

			for(ret = 0; ret < (gpio_string_dis?105:126); ret ++)
			{
				for(dis_ch = 0; dis_ch < 4; dis_ch ++)
				{
					uiDrawPoint(127 - ret, (GPIO_CH_VAL(ret, (3 - dis_ch) * 2) ? 3 : 13) + dis_ch * 16, 1);
					if(GPIO_CH_VAL(ret, (3 - dis_ch) * 2) != GPIO_CH_VAL(ret + 1, (3 - dis_ch) * 2))
					{
						uiDrawVLine(127 - ret, 4 + (dis_ch * 16), 9, 1);
					}
				}
			}
		}
		if(level_func_mode == 2)
		{	
			if(gpio_string_dis)
			{
				uiShowString(1, 5 + 0 * 16, "CH8", 8, 1);
				uiShowString(1, 5 + 1 * 16, "CH6", 8, 1);
				uiShowString(1, 5 + 2 * 16, "CH4", 8, 1);
				uiShowString(1, 5 + 3 * 16, "CH2", 8, 1);
			}

			if(gpio_back_dis)
			{
				for(ret = 0; ret < (gpio_string_dis?105:126); ret ++)
				{
					if(ret % 3 == 0)
					{
						uiDrawPoint(127 - ret, 8 + 0 * 16, 1);
						uiDrawPoint(127 - ret, 8 + 1 * 16, 1);
						uiDrawPoint(127 - ret, 8 + 2 * 16, 1);
						uiDrawPoint(127 - ret, 8 + 3 * 16, 1);
					}
				}
			}

			for(ret = 0; ret < (gpio_string_dis?105:126); ret ++)
			{
				for(dis_ch = 0; dis_ch < 4; dis_ch ++)
				{
					uiDrawPoint(127 - ret, (GPIO_CH_VAL(ret, (3 - dis_ch) * 2 + 1) ? 3 : 13) + dis_ch * 16, 1);
					if(GPIO_CH_VAL(ret, (3 - dis_ch) * 2 + 1) != GPIO_CH_VAL(ret + 1, (3 - dis_ch) * 2 + 1))
					{
						uiDrawVLine(127 - ret, 4 + (dis_ch * 16), 9, 1);
					}
				}
			}
		}

		memmove(GPIO_CH_DATA_S + 1, GPIO_CH_DATA_S, 126);
		
		if(uiKeyGetValue(UI_KEY_ENTER_ID) == DOWN)
			uiEnterAppSubList(0);
		if(uiKeyGetValue(UI_KEY_BACK_ID) == DOWN)
			uiBackAppLoop();
		if(uiKeyGetValue(UI_KEY_RIGHT_ID) == DOWN)
		{
			level_func_mode ++;
			if(level_func_mode > 2)
				level_func_mode = 0;
		}
		if(uiKeyGetValue(UI_KEY_LEFT_ID) == DOWN)
		{
			level_func_mode --;
			if(level_func_mode < 0)
				level_func_mode = 2;
		}
	}
}	

void gpio_pull_init_func(kkui_menu_event_e event, char* tab_name, void* param)
{
	if(event == ENTER_TAG)
	{
		menu_detect_level_channel_set[0].other_dis = (char *)gpio_pull_string[gpio_pull_set[0]];
		menu_detect_level_channel_set[1].other_dis = (char *)gpio_pull_string[gpio_pull_set[1]];
		menu_detect_level_channel_set[2].other_dis = (char *)gpio_pull_string[gpio_pull_set[2]];
		menu_detect_level_channel_set[3].other_dis = (char *)gpio_pull_string[gpio_pull_set[3]];
		menu_detect_level_channel_set[4].other_dis = (char *)gpio_pull_string[gpio_pull_set[4]];
		menu_detect_level_channel_set[5].other_dis = (char *)gpio_pull_string[gpio_pull_set[5]];
		menu_detect_level_channel_set[6].other_dis = (char *)gpio_pull_string[gpio_pull_set[6]];
		menu_detect_level_channel_set[7].other_dis = (char *)gpio_pull_string[gpio_pull_set[7]];
	}
}
void gpio_pull_set_func(kkui_menu_event_e event, char* tab_name, void* param)
{
	static uint8_t save_data;
	if(event == ENTER_TAG)
	{
		save_data = gpio_pull_set[tab_name[7] - '0' - 1];
	}
	if(event == LOOP_RUN)
	{
		if(uiKeyGetValue(UI_KEY_RIGHT_ID) == DOWN)
		{
			gpio_pull_set[tab_name[7] - '0' - 1] ++;
			if(gpio_pull_set[tab_name[7] - '0' - 1] > 2)
				gpio_pull_set[tab_name[7] - '0' - 1] = 0;
		}
		if(uiKeyGetValue(UI_KEY_LEFT_ID) == DOWN)
		{
			gpio_pull_set[tab_name[7] - '0' - 1] --;
			if(gpio_pull_set[tab_name[7] - '0' - 1] > 250)
				gpio_pull_set[tab_name[7] - '0' - 1] = 2;
		}
		if(uiKeyGetValue(UI_KEY_BACK_ID) == DOWN)
		{
			gpio_pull_set[tab_name[7] - '0' - 1] = save_data;
			uiBackAppLoop();
		}
		if(uiKeyGetValue(UI_KEY_ENTER_ID) == DOWN)
		{
			uiBackAppLoop();
		}
		menu_detect_level_channel_set[tab_name[7] - '0' - 1].other_dis = (char *)gpio_pull_string[gpio_pull_set[tab_name[7] - '0' - 1]];
	}
	if(event == BACK_LIST)
	{
		detect_level_gpio_init();
	}
}

void screen_extinction_func(kkui_menu_event_e event, char* tab_name, void* param)
{
	if(event == ENTER_TAG)
	{
		uiDisClear(0);
	}
	if(event == LOOP_RUN)
	{
		if(uiKeyGetValue(UI_KEY_BACK_ID) == DOWN || uiKeyGetValue(UI_KEY_ENTER_ID) == DOWN)
		{
			uiBackAppLoop();
		}
	}
	// if(event == BACK_LIST)
	// {
	// }
}

